package com.ultrapower.commons.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

/**
 * 
 * <p>description: 调用华为阻断接口的工具，协议类型为http，报文格式为json</p>
 * <p>Date:2017-5-2</p>
 * <p>modify：</p>
 * @author: huhui
 * @version: 1.0
 * </p>Company: 北京神州泰岳软件股份有限公司</p>
 */
public class HttpUtilDemo {
	
    public static String getToken(String postData, String postUrl) {
    	String token = "";
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();
            //System.out.println(conn.getResponseCode());
            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
//            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject jsonObject = new JSONObject(result);
            token = jsonObject.getString("access_token");
            //access_token
            in.close();
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return token;
    }
    
    public static String sendMsg(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("Content-Length", "" + postData.length());
            conn.setRequestProperty("Host", "171.0.0.4:7666");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");
            conn.setUseCaches(false);  
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setInstanceFollowRedirects(true);
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();
            System.out.println(conn.getResponseCode());
            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            System.out.println(result);
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }
    
    public static String stopNetwork(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("Content-Length", "" + postData.length());
            conn.setRequestProperty("Host", "171.0.0.4:7666");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");
            conn.setUseCaches(false);  
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setInstanceFollowRedirects(true);
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();
            System.out.println(conn.getResponseCode());
            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            System.out.println(result);
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

	public static void main(String[] args) {
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("telnum", "15872356969");
		map.put("smparam", "1 欢迎!");
		
//		String msgStr = JSON.toJSONString(map);
		String msgStr = new JSONObject(map).toString();
		
		Map<String, Object> stopNetworkMap=new HashMap<String,Object>();
		stopNetworkMap.put("servNumber", "15072196277");//15072494776//15072196277
		stopNetworkMap.put("recType", "StopSubs");
		stopNetworkMap.put("needPwd", "0");
		stopNetworkMap.put("changeReason", "reasontest");
		stopNetworkMap.put("authType", "AuthCheckB");
		stopNetworkMap.put("passWord", "");
		stopNetworkMap.put("certType", "IdCard");
		stopNetworkMap.put("certID", "");
		stopNetworkMap.put("oprRoam", "0");
		stopNetworkMap.put("IsRealSerSo", "0");
		stopNetworkMap.put("operatorID", "1101818");
//		stopNetworkMap.put("operatorID", "JK000001");
//		
//		String stopNetworkStr = JSON.toJSONString(stopNetworkMap);
		String stopNetworkStr = new JSONObject(stopNetworkMap).toString();
		
		String token = HttpUtilDemo.getToken("grant_type=client_credentials&client_id=20180927004540040&client_secret=35bfd69e1258585dc3eb7914e8ccd547","http://171.0.0.4:6011/OAuth/restOauth2Server/access_token");
		System.out.println(token);
		String url = "http://171.0.0.4:7666/OpenEbus/httpService/UserService?access_token="+token+"&method=sendSMSInfo&format=json";
		System.out.println(url);
		HttpUtilDemo.sendMsg(msgStr,url);
		String stopUrl = "http://171.0.0.4:7666/OpenEbus/httpService/UserService?access_token="+token+"&method=BLCSStopOpenSubs&format=json";
		System.out.println(stopUrl);
		HttpUtilDemo.stopNetwork(stopNetworkStr,stopUrl);
	}
}
